package com.webwork.mood.record.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.record.dao.DrawRecordDao;
import com.webwork.mood.record.dataobject.DrawRecord;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class DrawRecordDaoImpl extends SimpleDao implements DrawRecordDao {

	public DrawRecordDaoImpl() {
		super();
		this.dbTable = "t_draw_record";
		dbColumns.clear();
		dbColumns.put("userId", "USER_ID");
		dbColumns.put("awardName", "AWARD_NAME");
		dbColumns.put("drawDate", "DRAW_DATE");
		dbColumns.put("drawCount", "DRAW_COUNT");
		dbColumns.put("recCount", "REC_COUNT");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof DrawRecord))
			return new ColumnLimit();
		DrawRecord o = (DrawRecord) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(dbColumns.get("userId") + " = ");
		}
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(dbColumns.get("awardName") + " = ");
		}
		if (o.getDrawDate() != null) {
			ret.add(dbColumns.get("drawDate") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		DrawRecord o = (DrawRecord) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(o.getUserId());
		}
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(o.getAwardName());
		}
		if (o.getDrawDate() != null) {
			ret.add(o.getDrawDate());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		DrawRecord o = (DrawRecord) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getUserId());
		objs.add(o.getAwardName());
		objs.add(o.getDrawDate());
		objs.add(o.getDrawCount());
		objs.add(o.getRecCount());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		DrawRecord ret = new DrawRecord();
		ret.setUserId((String) map.get(dbColumns.get("userId")));
		ret.setAwardName((String) map.get(dbColumns.get("awardName")));
		ret.setDrawDate((Timestamp) map.get(dbColumns.get("drawDate")));
		ret.setDrawCount((Integer) map.get(dbColumns.get("drawCount")));
		ret.setRecCount((Integer) map.get(dbColumns.get("recCount")));
		return ret;
	}
	
	@Override
	public void insert(DrawRecord o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public List<DrawRecord> query(DrawRecord o, int page, int size) {
		List<DrawRecord> ret = new ArrayList<DrawRecord>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		int limIndex = sql.indexOf("LIMIT");
		if (limIndex >= 0) {
			StringBuffer stb = new StringBuffer(sql.substring(0, limIndex));
			stb.append("ORDER BY ");
			stb.append(dbColumns.get("drawDate"));
			stb.append(" DESC ");
			stb.append(sql.substring(limIndex));
			sql = stb.toString();
		} else {
			sql += " ORDER BY " + dbColumns.get("drawDate") + " DESC";
		}
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((DrawRecord) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(DrawRecord o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}

	@Override
	public List<DrawRecord> getUserDraw(String userId, String dateStr) {
		List<DrawRecord> ret = new ArrayList<DrawRecord>();
		String sql = "SELECT * FROM " + dbTable + " WHERE " + dbColumns.get("userId") + "=? AND " + dbColumns.get("drawDate") + " LIKE '" + dateStr + "%'";
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, userId);
		for (Map<String, Object> map : maps) {
			ret.add((DrawRecord) this.catchObject(map));
		}
		return ret;
	}
	
}
